<template>
  <div class="app-container">
    <el-row style="margin-top:20px;font-size:14px;color:#666">
      <el-col :span="4">客户：{{ order.real_name }}</el-col>
      <!-- <el-col :span="9">医院：{{ order.merchant_name }}</el-col> -->
      <el-col :span="5">项目：{{ project }}</el-col>
      <el-col :span="3">本金：{{ order.total_money }}</el-col>
      <!-- <el-col :span="3">总债权：{{ order.allmoney }}</el-col> -->
      <el-col :span="3">首打：{{ order.firstSend }}</el-col>
      <!-- <el-col :span="3">已结算：{{ order.settle_money }}</el-col> -->

    </el-row>

    <el-table  :key="tableKey" :summary-method="getSummaries" show-summary v-loading="listLoading" :data="list"  border fit highlight-current-row style="width: 100%;margin-top:20px">

      <el-table-column label="期数" align="center" >
        <template slot-scope="{row}">
          <span>{{ row.qs }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应还时间" align="center" >
        <template slot-scope="{row}">
          <span>{{ row.time_date }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="实际还款时间" align="center" width="110px">
          <template slot-scope="{row}">
            <span>{{ row.sh_time }}</span>
          </template>
        </el-table-column> -->
      <el-table-column label="还款状态" align="center" >
        <template slot-scope="{row}">
          <span>{{ row.status_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应还金额" align="center"  prop="total_money">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应还本金" align="center"  prop="money">
        <template slot-scope="{row}">
          <div class="input-box">
            <el-input v-model="row.money" size="small" prop="money" :disabled="row.is_paid==1?true:false" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="居间服务费" align="center"  prop="money_rate">
        <template slot-scope="{row}">
          <div class="input-box">
            <el-input v-model="row.money_rate" size="small" prop="money_rate" :disabled="row.is_paid==1?true:false" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="违约金" align="center" >
        <template slot-scope="{row}">
          <span>{{ row.late_fee }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button  type="primary" size="mini" @click="onSubmit(row)">
            保存
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  paymentList, paymentEditMediate
} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantList } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {},
  data() {
    return {
      tableKey: 0,
      tableRecord: 1,
      list: null,
      payway:[],
      order: [],
      options: [{
          value: 1,
          label: '是'
        }, {
          value: 0,
          label: '否'
        }],
      paid_options: [{
        value: 1,
        label: '是'
      }, {
        value: 0,
        label: '否'
      }, {
        value: 2,
        label: '部分'
      }],
      project: '',
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      query: {
        order_id: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      value1: '',
      value2: '',
      value3: ''
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.query.order_id = this.$route.params.order_id
      paymentList(this.query).then(response => {
        this.list = response.data.list
        this.order = response.data.order
        this.project = response.data.project
        this.listLoading = false
      })
    },
 
    onSubmit(row) {
      const pam = {}
      pam.money_rate = row.money_rate;
      pam.money = row.money;
      pam.pay_id = row.pay_id;

      paymentEditMediate(pam).then(resp => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
      })
    },
    getSummaries(param) {
      
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return (prev + curr);
              } else {
                return prev;
              }
            }, 0);
            
            sums[index]=sums[index].toFixed(0)
          } else {
            sums[index] = '/';
          }
        });
        console.log(sums)
        return sums;
      }
 

  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  select{
    width:100px;
  }
  .el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .tb-edit .input-box {
    display: none
  }
  .tb-edit .current-cell .input-box {
    display: block;
    margin-left: -15px;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
</style>
